package com.lanou.hotel.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanou.hotel.entity.Home;
import com.lanou.hotel.entity.Image;
import com.lanou.hotel.entity.response.HomeItem;
import com.lanou.hotel.mapper.HomeMapper;
import com.lanou.hotel.mapper.ImageMapper;
import com.lanou.hotel.service.IHomeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanou.hotel.utils.UUIDUtil;
import com.lanou.hotel.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author msy
 * @since 2023-10-28
 */
@Service
@Transactional
public class HomeServiceImpl extends ServiceImpl<HomeMapper, Home> implements IHomeService {

    @Autowired
    private HomeMapper homeMapper;
    @Autowired
    private ImageMapper imageMapper;

    /**
     * 更新房间
     * @param home
     * @throws Exception
     */
    @Override
    public void updateHome(Home home) throws RuntimeException{

        String homeId = home.getId();//房间id
        List<Image> imageList = home.getImageList();//获取图片路径集

        //封装图片集
        List<Image> images = new ArrayList<>();
        for(Image perImage : imageList){
            Image image = new Image();
            image.setId(UUIDUtil.getUUID());
            image.setUrl(perImage.getUrl());
            image.setPath(perImage.getPath());
            image.setHomeId(homeId);

            images.add(image);
        }

        homeMapper.updateById(home);
        images.forEach(image -> {
            imageMapper.addExcludeExists(image);
        });
    }

    /**
     * 分页条件查询房间列表
     * @param pageNum
     * @param pageSize
     * @param typeName
     * @return
     */
    @Override
    public ResultVO<Map<String, Object>> getHome(Integer pageNum, Integer pageSize, String typeName) {

        //通过mybatis plus分页插件获取条件查询后的page对象
        Page<HomeItem> page = new Page<>(pageNum, pageSize);

        //查询结果集
        List<HomeItem> homes = homeMapper.getHomes(page,typeName);

        Map map = (Map) new HashMap<String, Object>();
        map.put("total",page.getTotal());
        map.put("homes",homes);
        return ResultVO.success("查询成功",map);
    }

    /**
     * 保存房间信息 及 图片信息
     * @param home
     * @return
     */
    @Override
    public void saveHome(Home home) {

        List<Image> imageList = home.getImageList();
        String homeId = UUIDUtil.getUUID();
        home.setId(homeId);

        if (imageList.size() > 0 && imageList != null) {
            List<Image> images = new ArrayList<>();

            for(Image perImage : imageList){
                Image image = new Image();
                image.setId(UUIDUtil.getUUID());
                image.setUrl(perImage.getUrl());
                image.setPath(perImage.getPath());
                image.setHomeId(homeId);
                images.add(image);
            }

            imageMapper.saveImages(images);
        }

        homeMapper.saveHome(home);

    }

    @Override
    public void removeByIdAndRemoveImages(String homeId) {
        int count = homeMapper.deleteById(homeId);
        count += imageMapper.deleteByHomeId(homeId);
    }

    @Override
    public Home getOneByNum(Integer num) {
        return homeMapper.selectOneByNum(num);
    }
}
